package io.xiyou.user.service.impl;

import io.xiyou.user.api.IUserService;
import io.xiyou.user.api.bo.UpdateUserBO;
import io.xiyou.user.api.dto.UserDTO;
import io.xiyou.user.dao.mapper.UserMapper;
import io.xiyou.user.dao.model.User;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName：SentinelServiceImpl
 * @Author：西柚
 * @Date：2021-10-21 15:38:17
 * @Versiion：1.0
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据用户ID查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    @Cacheable(key = "#userId", value = "userCache")
    public UserDTO getByUserId(Long userId) {
        User user = userMapper.selectById(userId);
        if (Objects.isNull(user)) {
            return null;
        }

        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);

        return userDTO;
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @Override
    public List<UserDTO> list() {
        List<User> userList = userMapper.selectAll();
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        }

        List<UserDTO> userDTOList = new ArrayList<>();
        for (User user : userList) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user, userDTO);
            userDTOList.add(userDTO);
        }
        return userDTOList;
    }

    /**
     * 修改用户信息
     *
     * @param updateUserBO
     * @return
     */
    @Override
    @CachePut(key = "#updateUserBO.userId", value = "userCache")
    public UserDTO update(UpdateUserBO updateUserBO) {
        User user = userMapper.selectById(updateUserBO.getUserId());
        if (Objects.isNull(user)) {
            return null;
        }

        BeanUtils.copyProperties(updateUserBO, user);
        user.setUpdateDate(new Date());
        userMapper.updateById(user);

        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        return userDTO;
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @Override
    @CacheEvict(key = "#userId", value = "userCache", allEntries = true)
    public Integer delete(Long userId) {
        return userMapper.deleteById(userId);
    }

    /**
     * 计数器
     *
     * @return
     */
    @Override
    public Long counter(String key) {
        return redisTemplate.opsForValue().increment(key, 1);
    }

}
